package org.example.leetCode;

/**
 * @author Administrator
 */
public class ReverseList {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode pre = null;

        return reverse(head, pre);

    }

    private ListNode reverse(ListNode head, ListNode pre) {
        if (head == null) {
            return pre;
        }
        ListNode temp = head;
        head = head.next;
        temp.next = pre;
        pre = temp;
        return reverse(head, pre);
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
